/*
2021-8-8
https://www.acwing.com/problem/content/3799/
*/ 
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        string s;
        cin>>s;
        int len=s.size();
        int res=10;
        
        for(int i=0;i<(1<<len);i++)
        {
            int x=0;
            for(int j=0;j<len;j++)
                if(i>>j&1)
                    x=x*10+s[j]-'0';
            
            int t=sqrt(x);
            if(x && t*t==x) res=min(res,len-(int)to_string(x).size());
            
        }
        
        if(res==10) res=-1;
        cout<<res<<endl;
    }
    return 0;
}

